﻿using CoreModule;
using CoreModule.Views;
using Microsoft.Extensions.Logging;
using Prism.Ioc;
using Prism.Mvvm;
using System;
using System.Windows;
using System.Windows.Input;
using Yuwell.MedicalMultiParameter.App.Views;
using IdResourceModule.Views;
namespace IdResourceModule.ViewModels
{
    /// <summary>
    /// 
    /// </summary>
    public class MainWindowViewModel : BindableBase, IWindowLifetime
    {
  
        ILogger Logger { get; set; }
         
        /// <summary>
        /// 
        /// </summary>
        public ICommand ToggleModeCommand => new Prism.Commands.DelegateCommand(ToggleMode);
        /// <summary>
        /// 
        /// </summary>
        public ICommand SettingCommand => new Prism.Commands.DelegateCommand<FrameworkElement>(Setting);
        public ICommand ShowDocumentCommand => new Prism.Commands.DelegateCommand<FrameworkElement>(ShowDocument);
         
        public ICommand AboutCommand => new Prism.Commands.DelegateCommand<FrameworkElement>(About);
        
        public ICommand CloseWindowCommand => new Prism.Commands.DelegateCommand<FrameworkElement>(CloseWindow);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mask"></param>
        public void CloseWindow(FrameworkElement mask)
        {
            mask.Visibility = Visibility.Visible;
            var closeWindow = ContainerProvider.Resolve<CloseWindow>();
            
            if (closeWindow.ShowDialog() == true)
            {
                this.WindowClosing?.Invoke(this, EventArgs.Empty);
                Application.Current.Shutdown();
            }
            mask.Visibility = Visibility.Hidden; ;
        }
        /// <summary>
        /// 
        /// </summary>
        public void ToggleMode()
        {
            this.WindowMode = this.WindowMode == WindowMode.UI ? WindowMode.Management : WindowMode.UI;
            this.RaisePropertyChanged(nameof(WindowMode));
            this.WindowModeChanged?.Invoke(this, new WindowModeEventArgs() { WindowMode = this.WindowMode });
        }
        /// <summary>
        /// 
        /// </summary>
        public void About(FrameworkElement mask)
        {
            //mask.Visibility = Visibility.Visible;
            //mask.Visibility = Visibility.Hidden; ;
            AboutWindow window = new AboutWindow();
            window.ShowDialog();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mask"></param>
        public void ShowDocument(FrameworkElement mask)
        {
            try
            {
                var fileName = "血压仪程序使用说明书.docx";

                var filePath = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), fileName);
                if (System.IO.File.Exists(filePath))
                {
                    System.Diagnostics.Process.Start("explorer.exe", filePath);
                }
            }
            catch (Exception ex)
            {
               
            }
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mask"></param>
        public void Setting(FrameworkElement mask)
        {

            var  configurationHome = ContainerProvider.Resolve<Window>(CoreConstant.ConfigurationHomeWindowView);
             
           configurationHome.ShowDialog();
        }
         
        IContainerProvider ContainerProvider { get; set; }
        IDeviceManager DeviceManager { get; set; }
        IConfigurationManager ConfigurationManager { get; set; }
        public MainWindowViewModel(
                                IConfigurationManager configurationManager,
                                IDeviceManager deviceManager,
                                 
                                IContainerProvider containerProvider,
                                
                                ILogger logger)
        {
            ConfigurationManager= configurationManager;
            DeviceManager = deviceManager;
             
            ContainerProvider = containerProvider;
             
            Logger = logger;
            Logger.LogInformation("系统启动");
            ////
            ///
            DeviceManager.MeasurementDeviceStatusChanced += DeviceManager_MeasurementDeviceStatusChanced;
            DeviceManager.RecognitionDeviceStatusChanced += DeviceManager_RecognitionDeviceStatusChanced;
            DeviceManager.RecognitionDeviceFound += DeviceManager_RecognitionDeviceFound;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeviceManager_RecognitionDeviceFound(object? sender, EventArgs e)
        {
            this.RecognitionDeviceVisibility = Visibility.Visible;
            this.RaisePropertyChanged(nameof(this.RecognitionDeviceVisibility));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeviceManager_RecognitionDeviceStatusChanced(object? sender, DeviceStatusEventArgs e)
        {
            this.RecognitionDeviceStatus = e.DeviceStatus;
            this.RaisePropertyChanged(nameof(this.RecognitionDeviceStatus));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeviceManager_MeasurementDeviceStatusChanced(object? sender, DeviceStatusEventArgs e)
        {
            this.MeasurementDeviceStatus = e.DeviceStatus;
            this.RaisePropertyChanged(nameof(this.MeasurementDeviceStatus));
        }

        public void ShowMask()
        {
            throw new NotImplementedException();
        }

        public void CloseMask()
        {
            throw new NotImplementedException();
        }

        public void HighlightMeasurementListMenu()
        {
            throw new NotImplementedException();
        }

        public WindowMode WindowMode { get; set; } = WindowMode.UI;
        public Visibility _measurementDeviceVisibility { get; set; }
        public Visibility MeasurementDeviceVisibility {
            get { return _measurementDeviceVisibility; }
            set { _measurementDeviceVisibility = value;
                RaisePropertyChanged(nameof(this.MeasurementDeviceVisibility));
            } }
        public Visibility _managementVisibility = Visibility.Collapsed;
        /// <summary>
        /// 
        /// </summary>
        public Visibility ManagementVisibility {
            get { return _managementVisibility; }
            set { _managementVisibility = value;
                RaisePropertyChanged(nameof(this.ManagementVisibility)); } }
        public Visibility _recognitionDeviceVisibility = Visibility.Collapsed;
        public Visibility RecognitionDeviceVisibility
        {
            ///默认显示
            get { return Visibility.Visible; }
            set
            {
                _recognitionDeviceVisibility = value;
                RaisePropertyChanged(nameof(this.RecognitionDeviceVisibility));
            }
        }
        public DeviceStatus _measurementDeviceStatus = DeviceStatus.Disconnected;
        public DeviceStatus MeasurementDeviceStatus
        {
            get { return _measurementDeviceStatus; }
            set
            {
                _measurementDeviceStatus = value;
                RaisePropertyChanged(nameof(this.MeasurementDeviceStatus));
            }
        }
        public DeviceStatus _recognitionDeviceStatus = DeviceStatus.Disconnected;
        public DeviceStatus RecognitionDeviceStatus
        {
            get { return _recognitionDeviceStatus; }
            set
            {
                _recognitionDeviceStatus = value;
                RaisePropertyChanged(nameof(this.RecognitionDeviceStatus));
            }
        }
        /// <summary>
        /// 
        /// </summary>
        RegionViewKind _regionViewKind = RegionViewKind.None;
        /// <summary>
        /// 
        /// </summary>
        public RegionViewKind RegionViewKind { get {
                return _regionViewKind;
            } set {
            
                if (value != _regionViewKind)
                {
                    _regionViewKind = value;
                    this.RegionViewKindChanged?.Invoke(this, EventArgs.Empty);
                }
            } }
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<EventArgs> WindowClosing;
        public event EventHandler<WindowModeEventArgs> WindowModeChanged;
        public event EventHandler<EventArgs> RegionViewKindChanged;
    }
}
